
// npx babel-node regexp_demo01.js

// 正确返回字符串长度的函数
function codePointLength(text) {
    let result = text.match(/[\s\S]/gu);
    return result ? result.length : 0;
}

let test01 = function() {
    let s = '𠮷𠮷';

    let _ = s.length;
    console.log(_);
    _ = codePointLength(s);
    console.log(_);
};

// test01();

let test02 = function() {
    const r1 = /hello/;
    const r2 = /hello/u;

    let _ = r1.unicode;
    console.log(_);
    _ = r2.unicode;
    console.log(_);
};

// 这里的运行结果和文档上的不一致！
// test02();

// y修饰符

function tokenize(TOKEN_REGEX, str) {
    let result = [];
    let match;
    while(match = TOKEN_REGEX.exec(str)) {
        result.push(match[1]);
    }
    return result;
}

let test03 = function(){
    const TOKEN_Y = /\s*(\+|[0-9]+)\s*/y;
    const TOKEN_G = /\s*(\+|[0-9]+)\s*/g;

    let _ = tokenize(TOKEN_Y, '3 + 4');
    console.log(_);

    _ = tokenize(TOKEN_G, '3 + 4');
    console.log(_);

    _ = tokenize(TOKEN_Y, '3x + 4');
    console.log(_);

    _ = tokenize(TOKEN_G, '3x + 4');
    console.log(_);
};


test03();












